local joy__shenglun = fk.CreateSkill {

  name = "joy__shenglun",

  tags = {  },

}



joy__shenglun:addEffect("active", {
  name = "joy__shenglun",
  anim_type = "control",
  card_num = 0,
  min_target_num = 1,
  max_target_num = 2,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected < 2 and player.id ~= to_select
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(joy__shenglun.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:sortPlayersByAction(effect.tos)
    for _, pid in ipairs(effect.tos) do
      local to = room:getPlayerById(pid)
      local list = {
        player.hp - to.hp,
        player:getHandcardNum() - to:getHandcardNum(),
        #player:getEquipments(Card.SubtypeWeapon)-#to:getEquipments(Card.SubtypeWeapon),
        #player:getEquipments(Card.SubtypeArmor)-#to:getEquipments(Card.SubtypeArmor),
        (#player:getEquipments(Card.SubtypeDefensiveRide)+#player:getEquipments(Card.SubtypeOffensiveRide)) -
        (#to:getEquipments(Card.SubtypeDefensiveRide)+#to:getEquipments(Card.SubtypeOffensiveRide)),
      }
      for _, n in ipairs(list) do
        if n > 0 then
          room:addPlayerMark(player, "joy__shenglun_win")
        else
          room:addPlayerMark(player, "joy__shenglun_lose")
        end
      end
    end
    checkShenglunMark(player)
    local yiji = Fk.skills["joyex__yiji"]
    if player:getMark("joy__shenglun_win") > 9 then
      if player:isWounded() then
        room:recover { num = 1, skillName = joy__shenglun.name, who = player, recoverBy = player}
        if player.dead then return end
      end
      room:useSkill(player, yiji, function()
        return yiji:use(fk.Damaged, player, player, {to = player, num = 1})
      end)
      room:setPlayerMark(player, "joy__shenglun_win", 0)
      checkShenglunMark(player)
    end
    if player.dead then return end
    if player:getMark("joy__shenglun_lose") > 9 then
      local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#joy__shenglun-damage", joy__shenglun.name, false)
      room:damage { from = player, to = room:getPlayerById(tos[1]), damage = 1, skillName = joy__shenglun.name }
      if player.dead then return end
      room:useSkill(player, yiji, function()
        return yiji:use(fk.Damaged, player, player, {to = player, num = 1})
      end)
      room:setPlayerMark(player, "joy__shenglun_lose", 0)
      checkShenglunMark(player)
    end
  end,
})

return joy__shenglun